package net.slenderloris.TouchServer;

import java.awt.Frame;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import net.slenderloris.TouchServer.GestureEventGenerator.OnTapListener;

public class TouchServerLanMain {

	private static final int ACTION_UP = 0x1;
	private static final int ACTION_DOWN = 0x2;
	private static final int ACTION_MOVE = 0x3;
	
	private ServerSocket mServerSocket;
	private FingerCanvas mFingerCanvas;
	private GestureEventGenerator mGestureEventGenerator;

	private class ListenRunnable implements Runnable {
		@Override
		public void run() {
			boolean run = true;
			while(run) {
				try {
					System.out.println("Getting socket");
					Socket server = mServerSocket.accept();
					System.out.println("Just connected to " + server.getRemoteSocketAddress());
					System.out.println("Got socket");
					InputStream is = server.getInputStream();
					DataInputStream dis = new DataInputStream(is);
					while(run) {
						byte action = dis.readByte();
						byte id = dis.readByte();
						int time = dis.readInt();
						short x = dis.readShort();
						short y = dis.readShort();

						String actionStr = actionToString(action);
						
						System.out.println(actionStr + " (" + id + ") " + x + "x" + y + " [" + time + "]");
						
						switch(action) {
						case ACTION_DOWN:
							mGestureEventGenerator.fingerDown(id, time, x, y);
						case ACTION_MOVE:
							mFingerCanvas.setPosition(id, x, y);
							break;
						case ACTION_UP:
							mGestureEventGenerator.fingerUp(id, time, x, y);
							mFingerCanvas.fingerUp(id);
							break;
						}
					}
					dis.close();
					is.close();
				} catch (IOException e) {
					run = false;
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		System.out.println("Hello World!"); // Display the string.
		TouchServerLanMain server = new TouchServerLanMain();
		server.start();
	}
		
	public void start() {
		
		int port = -1;
		
		try {
		    mServerSocket = new ServerSocket(57392);
		    mServerSocket.setSoTimeout(100000);
		    port = mServerSocket.getLocalPort();
		    System.out.println("Opened socked on port " + port + " at Address " + mServerSocket.getInetAddress() );
			new Thread(new ListenRunnable()).start();
		} 
		catch (IOException e) {
		    System.out.println("Could not listen on port: " + port);
		    e.printStackTrace();
		    System.exit(-1);
		}
		
		mFingerCanvas = new FingerCanvas();
		
		mGestureEventGenerator = new GestureEventGenerator();
		mGestureEventGenerator.setOnTapListener(new OnTapListener() {
			@Override
			public void onTap(int x, int y) {
				mFingerCanvas.setTapPoint(x,y);
			}
		});
		
		Frame aFrame = new Frame(); 
		aFrame.setSize(1280, 800); 
		//add the canvas 
		aFrame.add(mFingerCanvas);
		aFrame.setVisible(true);
		aFrame.invalidate();
		
	}

	public static String actionToString(byte action) {
		switch(action) {
		case ACTION_UP: return "Up";
		case ACTION_DOWN: return "Down";
		case ACTION_MOVE: return "Move";
		}
		return null;
	}

}
